package com.atguigu.biliplayer.ui;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.atguigu.biliplayer.R;
import com.atguigu.biliplayer.adapter.MainPagerAdapter;
import com.atguigu.biliplayer.fragments.AttentionFragment;
import com.atguigu.biliplayer.fragments.DiscoverFragment;
import com.atguigu.biliplayer.fragments.OperaFragment;
import com.atguigu.biliplayer.fragments.RecommendFragment;
import com.atguigu.biliplayer.fragments.SubareaFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主页基类
 * Created by 刘晨龙 on 2015/11/5
 */
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {


    private List<Map<String,Object>> mFragments;
    private RelativeLayout mCommonToolbar;
    private NavigationView mNavigationView;
    private DrawerLayout mDrawerLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initNavigation();
        initToolbar();
        initFragments();
        initViewPagerAndTabs();
    }



    /**
     * 实例化所有的Fragment
     * @author 刘晨龙
     */
    private void initFragments() {
        Fragment operaFragment = new OperaFragment();
        Fragment recommendFragment = new RecommendFragment();
        Fragment subareaFragment = new SubareaFragment();
        Fragment attentionFragment = new AttentionFragment();
        Fragment discoverFragment = new DiscoverFragment();
        setFragment(operaFragment,"番剧");
        setFragment(recommendFragment,"推荐");
        setFragment(subareaFragment,"分区");
        setFragment(attentionFragment,"关注");
        setFragment(discoverFragment,"发现");
    }

    /**
     * 初始化导航栏
     *
     * @author 刘晨龙
     */
    private void initNavigation() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mNavigationView = (NavigationView) findViewById(R.id.nav_view);
        mNavigationView.setNavigationItemSelectedListener(this);
    }

    /**
     * 初始化toolbar
     *
     * @author 刘晨龙
     */
    private void initToolbar() {
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
        assignToolbarViews();
        mToolbar.addView(mCommonToolbar);
    }
    ///////////////////////////////////////////////////////////////////////////
    // Toolbar操作对外开放
    ///////////////////////////////////////////////////////////////////////////
    public ImageView mDrawerHome;
    public ImageView mDefaultAvatar;
    public ImageView mGameCenter;
    public ImageView mMenuDownload;
    public ImageView mMenuSearch;

    /**
     * 初始化toolbar
     */
    private void assignToolbarViews() {
        mCommonToolbar = (RelativeLayout) View.inflate(this, R.layout.nav_common_toolbar, null);
        mDrawerHome = (ImageView) mCommonToolbar.findViewById(R.id.drawer_home);
        mDefaultAvatar = (ImageView) mCommonToolbar.findViewById(R.id.default_avatar);
        mGameCenter = (ImageView) mCommonToolbar.findViewById(R.id.game_center);
        mMenuDownload = (ImageView) mCommonToolbar.findViewById(R.id.menu_download);
        mMenuSearch = (ImageView) mCommonToolbar.findViewById(R.id.menu_search);
        mDrawerHome.setOnClickListener(this);
    }


    /**
     * 获取各个便签页，他们是Fragment
     * @author 刘晨龙
     */
    private void setFragment(Fragment fragment,String title) {
        if (mFragments == null ){
            mFragments = new ArrayList<>();
        }
        if (fragment != null) {
            Map<String,Object> map = new HashMap<>();
            map.put("fragment",fragment);
            map.put("title",title);
            mFragments.add(map);
        }
    }

    /**
     * 初始化ViewPagerAndTabs
     *
     * @author 刘晨龙
     */
    private void initViewPagerAndTabs() {
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        MainPagerAdapter pagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
        if (mFragments != null && !mFragments.isEmpty()) {
            for (int i = 0; i < mFragments.size(); i++) {
                Map<String, Object> map = mFragments.get(i);
                pagerAdapter.addFragment((Fragment)map.get("fragment"),(String)map.get("title"));
            }
        }
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(1);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    /**
     * 打开或者关闭Nav
     */
    private void openOrCloseNav() {
        if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
            mDrawerLayout.closeDrawer(GravityCompat.START);
        }else {
            mDrawerLayout.openDrawer(GravityCompat.START);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.drawer_items, menu);
        return true;
    }

    /**
     * 菜单的响应时间
     *
     * @param item
     * @return
     * @ author 刘晨龙
     */
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // 获取id
        int id = item.getItemId();

        if (id == R.id.nav_home) {
            //首页
            // Handle the camera action
        } else if (id == R.id.nav_offline_manager) {
            //离线管理

        } else if (id == R.id.nav_favorites) {
            //我的收藏

        } else if (id == R.id.nav_histories) {
            //历史记录

        } else if (id == R.id.nav_following) {
            //关注的人

        } else if (id == R.id.nav_pay) {
            //消费记录

        } else if (id == R.id.nav_theme) {
            //主题选择

        } else if (id == R.id.nav_settings) {
            //设置

        } else if (id == R.id.nav_help) {
            //帮助与反馈

        }
        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.drawer_home:
                openOrCloseNav();
                break;
        }
    }
}
